﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ZHDJ.Core.Caching;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Box;

namespace SysManage.Module.Menu
{
    public partial class PortalMenuEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PortalMenuEdit";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();

            string id = GetQueryIntValue("id");
            PortalMenu current = DB.PortalMenus.Include(m => m.Parent).Where(m => m.ID == id).FirstOrDefault();

            if (!string.IsNullOrEmpty(id))
            {
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtName.Text = current.Name;
                txtUrl.Text = current.NavigateUrl;
                txtOrderNo.Text = current.OrderNo.ToString();
                txtRemark.Text = current.Remark;
            }

            // 绑定上级菜单下拉列表
            BindDDL(current);
        }

        private void BindDDL(PortalMenu current)
        {
            if (current == null)
            {
                List<PortalMenu> menus = ResolveDDL<PortalMenu>(PortalMenuHelper.PortalMenus);

                // 绑定到下拉列表（启用模拟树功能）
                ddlParent.EnableSimulateTree = true;
                ddlParent.DataTextField = "Name";
                ddlParent.DataValueField = "ID";
                ddlParent.DataSimulateTreeLevelField = "TreeLevel";
                ddlParent.DataSource = menus;
                ddlParent.DataBind();

                // 选中根节点
                ddlParent.SelectedValue = "0";
            }
            else
            {
                List<PortalMenu> mys = ResolveDDL<PortalMenu>(PortalMenuHelper.PortalMenus, current.ID);

                // 绑定到下拉列表（启用模拟树功能和不可选择项功能）
                ddlParent.EnableSimulateTree = true;
                ddlParent.DataTextField = "Name";
                ddlParent.DataValueField = "ID";
                ddlParent.DataSimulateTreeLevelField = "TreeLevel";
                ddlParent.DataEnableSelectField = "Enabled";
                ddlParent.DataSource = mys;
                ddlParent.DataBind();

                if (current.Parent != null)
                {
                    // 选中当前节点的父节点
                    ddlParent.SelectedValue = current.Parent.ID.ToString();
                }
            }
        }

        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            string id = GetQueryIntValue("id");

            if (!string.IsNullOrEmpty(id))
            {
                PortalMenu item = DB.PortalMenus.Include(m => m.Parent).Where(m => m.ID == id).FirstOrDefault();
                item.Name = txtName.Text.Trim();
                item.NavigateUrl = txtUrl.Text.Trim();
                item.OrderNo = Convert.ToInt32(txtOrderNo.Text.Trim());
                item.Remark = txtRemark.Text.Trim();

                string parentID = Convert.ToString(ddlParent.SelectedValue);
                if (parentID == "-1")
                {
                    item.Parent = null;
                }
                else
                {
                    PortalMenu menu = Attach<PortalMenu>(parentID);
                    item.Parent = menu;
                }

                DB.SaveChanges();
            }
            else
            {
                PortalMenu item = new PortalMenu();
                item.ID = StringHelper.NewID;
                item.Name = txtName.Text.Trim();
                item.NavigateUrl = txtUrl.Text.Trim();
                item.OrderNo = Convert.ToInt32(txtOrderNo.Text.Trim());
                item.Remark = txtRemark.Text.Trim();

                string parentID = Convert.ToString(ddlParent.SelectedValue);
                if (parentID == "-1")
                {
                    item.Parent = null;
                }
                else
                {
                    PortalMenu menu = Attach<PortalMenu>(parentID);
                    item.Parent = menu;
                }

                DB.PortalMenus.Add(item);

                DB.SaveChanges();
            }
            MemoryCacheManager mCache = new MemoryCacheManager();
            mCache.Remove(CacheKeys.Cache_Menu);
            //FineUI.Alert.Show("保存成功！", String.Empty, FineUI.Alert.DefaultIcon, FineUI.ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}